{template header}
<style>
.lottery-mask {position: fixed;width: 100%;height: 100%;left: 0; top: 0; background: rgba(0, 0, 0, 0.5); text-align: center;}
.lottery-mask .close { 
    position: absolute; right: 5px;  top: 5px;
}
.lottery-mask .popup {
  position: fixed; max-height: 300px; overflow-y:visible; overflow-x: hidden;
  padding: 20px;  line-height: 30px;  background: #fff;  border: 5px solid #f1c40f;  color: #5e5e5e;  font-size: 15px;  width: 260px;
  top: 50%;  left:50%;  margin: -150px 0 0 -140px;
}

.lottery-mask .popup ul {width: 100%;  margin: 0;padding: 0;}
.lottery-mask .popup li {margin: 0; list-style: none; padding: 0;  text-align: left; margin-top: 10px;} 
.lottery-mask .popup li span { display:inline-block; vertical-align:middle; width:100px;}
.lottery-mask .popup li input,.lottery-mask .popup li select {  border: 1px solid #f1c40f; padding: 0 px;  background: #fff6d4;  outline: none;  height: 30px;
  margin-left: 5px;  width: 143px;}
.lottery-mask .popup a {  width: 70%;}
.lottery-btn { 
    color: #ff6900;  border: 1px solid #ff6900;  height: 44px;  line-height: 44px;  margin: 6px auto;  border-radius: 22px;  display: inline-block;
    vertical-align: middle;  text-decoration: none;  margin-top: 20px;
}
	
</style>
<h4 style="padding-left:10px;">
    <span>
        {$actTitle}—抽奖码概要&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/?c=actLottery&a=lotterylist">返回列表页
        </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/?c=actLottery&a=lotteryPrizeUserList&lid={$lid}">中奖用户列表</a>
    </span>
</h4>
<h6>
    已有抽奖码：{$codeTotal} &nbsp;&nbsp;已领抽奖码：{$codeSend} &nbsp;&nbsp;可领抽奖码：{eval echo $codeTotal -  $codeSend}
</h6>

<hr style="clear:both;" />

<h4 style="padding-left:10px;">
    <span>生成抽奖码</span>
</h4>
<div class="form-horizontal" >
    <div class="search-group" style="margin:0;" >
        <label class="control-label" style="text-align:left; text-indent:12px; " >数量</label>
        <div class="controls">
          <input type="text" placeholder="数量" value="500" style="width: 100px;" class="code-num">
        </div>
    </div>

    <div class="search-group" style="margin:0;" >
        <div class="controls">
            <a class="btn code-generate">生成</a>　
        </div>
    </div>
</div>

<hr style="clear:both;" />

<h4 style="padding-left:10px;">
    <span>设置抽奖码中奖信息</span>
</h4>
<p>
    ID区间：
    <input type="text" class="gene-idsub" style="width: 100px;" value="" placeholder="ID"> &nbsp;至  &nbsp;
    <input type="text" class="gene-idsup" style="width: 100px;" value="" placeholder="ID">(该项只有一个值将忽略下面的顺序和数量项)
</p>
<p>
    顺序：
    <select name="gene-order" style="width: 100px;">
        <option value="1">正序(id从小到大)</option>
        <option value="2">倒序(id从大到小)</option>
        <option value="3">随机</option>
    </select>

    数量：<input type="text" class="gene-num" style="width: 100px;" value="" placeholder="数量">

    奖项：
    <select name="gene-prize" style="width: 100px;">
    </select>

    <a class="btn gene-go" style="margin-top:-10px;margin-left:20px">设置</a>　
</p>
<hr style="clear:both;" />

<h4 style="padding-left:10px;">
    <span>抽奖码列表</span>
</h4>
<div class="bs-docs-example" style="clear:both;">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>抽奖码</th>
                <th>是否已领取</th>
                <th>奖项</th>
                <th>是否已抽奖</th>
                <th>领取时间</th>
                <th>领取人ID</th>
                <th>领取人姓名</th>
                <th>领取人手机</th>
                <th>编辑</th>
            </tr>
        </thead>
        <tbody>
            {loop $codeList $k $v}
                <tr>
                    <td>{$v['id']}</td>
                    <td>{$v['code']}</td>
                    <td>{if $v['ucid']}是{else}否{/if}</td>
                    <td>{$v['prize_desc']}</td>
                    <td>{if $v['is_draw']}是{else}否{/if}</td>
                    <td>{eval if ($v['receive_time']) echo date('Y-m-d H:i:s', $v['receive_time']); else echo '/';}</td>
                    <td>{$v['uid']}</td>
                    <td>{$v['uname']}</td>
                    <td>{$v['phone']}</td>
                    <td><a class="btn set-award" data-id="{$v['id']}" data-uid="{$v['uid']}" data-phone="{$v['phone']}">奖项设置</a></td>
                </tr>
            {/loop}
        </tbody>
    </table>
</div>
<div class="pagination">
    <ul>
        {$pageStr}
    </ul>
</div>
<div class="lottery-mask" style="display: none;">
    <section class="popup">
        <span class="close">X</span>
        <div class="form-code">
        </div>
    </section>
</div>
<script type="text/javascript">
    $(function(){
        var lid = {$lid};
        var awardLevel = '{$awardLevel}';
        //生成抽奖码
        $('.code-generate').on('click', function(){
            $.post(
                '/actLottery/generateCode',
                {'lid': lid, 'num': $('.code-num').val()},
                function(data){
                    if (data.code == 0) {
                            alert(data.msg); 
                            location.reload();
                    } else {
                            alert(data.msg);
                    }
                },
                'json'
            )
        });
        
        $('.set-award').on('click', function(){
            //领取人ID
            var uid = $(this).attr('data-uid');
            var phone = $(this).attr('data-phone');
            var popStr = '<ul><li><span>奖项：</span><select name="prize_level">';
            popStr += getPrizeOption();
            popStr += '</select></li></ul>';
            
            if (uid != '') {
                popStr += '<ul><li><span>用户UID：</span>' + uid + '</li></ul>';
            } else {
                popStr += '<ul><li><span>用户UID：</span><input name="uid" value="" /></li></ul>';
            }
            
            if (phone != '') {
                popStr += '<ul><li><span>用户手机：</span>' + phone + '</li></ul>';
            } else {
                popStr += '<ul><li><span>用户手机：</span><input name="phone" value="" /></li></ul>';
            }
            
            popStr += '<a href="javascript:void(0);" class="lottery-btn" data-id="'+$(this).attr('data-id')+'">提交</a>';
            $('.form-code').html(popStr);
            $('.lottery-mask').show();
        });
        
        $('.close').on('click', function() {
            $('.lottery-mask').hide();
            $('.form-code').html('');
        });
        
        $('.form-code').on('click', '.lottery-btn' ,function() {
            var al = $('select[name="prize_level"]').val();
            var uid = $('input[name="uid"]').val();
            if (uid == undefined) {
                uid = '';
            }
            
            var phone = $('input[name="phone"]').val();
            if (phone == undefined){
                phone = '';
            }
            
            if ((uid && phone == '') || (phone && uid == '')) {
                alert('手机号和用户ID必须同时设置');
                return false;
            }
            
            $.post(
                '/actLottery/updateCodeInfo',
                {'prize_level': al, 'id': $(this).attr('data-id'), 'uid': uid, 'phone': phone, 'lid': lid},
                function(data){
                    if (data.code == 0) {
                            alert(data.msg); 
                            location.reload();
                    } else {
                            alert(data.msg);
                    }
                },
                'json'
            )
        });
        
        //抽奖码中奖信息设置
        $('select[name="gene-prize"]').html(getPrizeOption());
        
        $('.gene-go').on('click', function(){
            var subNum = $('.gene-idsub').val();
            var supNum = $('.gene-idsup').val();
            var gOrder = $('select[name="gene-order"]').val();
            var gCount = $('.gene-num').val()
            var gPrize = $('select[name="gene-prize"]').val();
            
            if (!subNum && !supNum && !gCount) {
                alert('参数有误,ID区间和数量必须有一个有有效值');
                return false;
            }
            
            $.post(
                "/actLottery/prizeSet",
                {'idSub': subNum, 'idSup': supNum, 'gOrder': gOrder, 'gCount': gCount, 'gPrize': gPrize, 'lid':{$lid}},
                function(data){
                    alert(data.msg);
                    
                    if (data.code == 0) {
                        location.reload();
                    }
                },
                'json'
            );
        });
        
        //奖项option选项
        function getPrizeOption() {
            var aLevel = JSON.parse(awardLevel);
            var optStr = '<option value="0">无</option>';
            
            for (var i=0; i < aLevel.length; i++) {
                optStr += '<option value=' + (i+1) + '>' + aLevel[i] + '</option>';
            }
            
            return optStr;
        }
    })
</script>
{template footer}
